using System;
using System.Collections.Generic;
using System.Text;

namespace HumanElement.Xna.Construct.GameState
{
    public class MenuItemCollection : System.Collections.ObjectModel.Collection<MenuItem>
    {
        private MenuScreen owner;

        public MenuItemCollection(MenuScreen owner)
        {
            if (owner == null) throw new ArgumentNullException("owner");
            this.owner = owner;
        }

        protected override void InsertItem(int index, MenuItem item)
        {
            int existingIndex = this.IndexOf(item);

            if (existingIndex >= 0)
            {
                this.Remove(item);
                if (index > existingIndex)
                {
                    index -= 1;
                }
            }
            base.InsertItem(index, item);
            item.Parent = owner;
            this.owner.OnItemAdded(item);
        }

        protected override void RemoveItem(int index)
        {
            MenuItem item = this.Items[index];
            base.RemoveItem(index);
            item.Parent = null;
        }
    }
}
